<template>
  <div class="attendanceRecord-page">
    <breadcrumb />
    <div class="search-container">
      <Search @search="getSearchParams" />
    </div>
    <div class="table-container">
      <div class="table-component">
        <div class="submit-btns">
          <div></div>
          <div class="submit-btns-right">
            <el-button
              type="primary"
              class="submit-btn-export"
              @click="exportData"
            >
              <img class="icon-img" :src="iconExport" alt="" />导出</el-button
            >
          </div>
        </div>
        <div class="table">
          <el-table
            :data="table.data"
            :loading="table.loading"
            style="width: 100%"
            :header-cell-style="{
              background: '#F1F1F1',
              'font-size': '14px',
              'font-weight': 600,
              color: '#333333',
            }"
            :cell-style="{
              'font-size': '14px',
              'font-weight': 400,
              color: '#333333',
            }"
          >
            <el-table-column label="序号" min-width="60" fixed="left">
              <template #default="scope">
                {{ (scope.$index + 1).toString().padStart(2, "0") }}
              </template>
            </el-table-column>
            <el-table-column prop="teamGroup" label="班组名称" min-width="80" />
            <el-table-column
              prop="realName"
              label="收费员姓名"
              min-width="100"
            />
            <el-table-column prop="userName" label="账号" min-width="60" />
            <el-table-column label="考勤结果" min-width="90">
              <template #default="scope">
                <span v-if="scope.row.status == 1" class="blue">正常</span>
                <span v-if="scope.row.status == 2" class="red">迟到</span>
                <span v-if="scope.row.status == 3" class="red">早退</span>
                <span v-if="scope.row.status == 4" class="red">缺卡</span>
                <span v-if="scope.row.status == 5" class="grey">无排版</span>
                <span v-if="scope.row.status == 23" class="red">迟到-早退</span>
                <span v-if="scope.row.status == 24" class="red">迟到-缺卡</span>
              </template>
            </el-table-column>
            <el-table-column prop="sections" label="登记路段" min-width="220" />
            <el-table-column prop="inTime" label="签到时间" min-width="170" />
            <el-table-column prop="outTime" label="签退时间" min-width="170" />
            <el-table-column label="操作" width="60" fixed="right">
              <template #default="scope">
                <el-button
                  @click="details(scope.row)"
                  type="primary"
                  size="small"
                  link
                  style="color: #007aff; font-size: 14px"
                  >详情</el-button
                >
              </template>
            </el-table-column>
            <template #empty>
              <el-empty :image="imgNone" />
            </template>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="pagination.pageSizes"
            layout="total,sizes, prev, pager, next, jumper"
            :total="table.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
        <el-dialog
          v-model="dialogVisible"
          :title="title"
          destroy-on-close
          :before-close="handleClose"
          :close-on-click-modal="false"
          width="390"
        >
          <component
            :is="componentTag"
            @operationDone="operationDone"
            :data="operationId"
          >
          </component>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script setup>
import breadcrumb from "@/components/breadcrumb/index.vue";
import Search from "./components/search.vue";
import pictureComponent from "./components/pictureComponent.vue";
import iconExport from "@/assets/image/btn/icon-export.png";
const imgNone = new URL("@/assets/image/imgNone.png", import.meta.url).href;
import { reactive, ref, shallowRef } from "vue";
import {
  getSignIn,
  getExportSignIn,
} from "./request.js";

// 搜索
const searchModel = ref({
  keyword: "",
  teamId: "",
  status: "",
  startTime: "",
  endTime: "",
});
const getSearchParams = (value) => {
  // searchParams.value = value;
  searchModel.value = value;
  getData();
};

// 弹窗
const title = ref("");
const dialogVisible = ref(false);
const componentTag = shallowRef("");
const operationId = ref("");
const operationDone = (r) => {
  dialogVisible.value = false;
  operationId.value = "";
  if (!r) {
    getData();
  }
};

// 详情按钮
const details = (row) => {
  title.value = "签到照片";
  operationId.value = row;
  componentTag.value = pictureComponent;
  dialogVisible.value = true;
};

// 导出按钮
const exportData = () => {
  ElMessageBox.confirm(`您确认要导出数据？`, "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await getExportSignIn({
      keyword: searchModel.value.keyword,
      teamId: searchModel.value.teamId,
      status: searchModel.value.status,
      startTime: searchModel.value.startTime,
      endTime: searchModel.value.endTime,
    })
      .then((res) => {
        const fileName = extractFileNameFromHeaders(res.headers);
        const url = window.URL.createObjectURL(new Blob([res.data]));
        const link = document.createElement("a");
        link.href = url;
        link.download = fileName;
        link.click();
      })
      .catch((err) => {
        console.log("err ===>", err);
      });
  });
};

function extractFileNameFromHeaders(headers) {
  const contentDisposition = headers.get("content-disposition") || "";

  const matches = contentDisposition.match(/filename\*=\s*utf-8''([^;]+)/);

  if (matches != null && matches[1]) {
    return decodeURIComponent(matches[1].replace('"', ""));
  }
  return "unknown-file.pdf"; // 默认文件名
}

// 表格
const table = reactive({
  data: [],
  total: 0,
  loading: true,
});

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  pageSizes: [10, 20, 30, 50],
});

const handleCurrentChange = (value) => {
  pagination.currentPage = value;
  getData();
};

const handleSizeChange = (value) => {
  pagination.pageSize = value;
  getData();
};

const getData = async () => {
  await getSignIn({
    keyword: searchModel.value.keyword,
    teamId: searchModel.value.teamId,
    status: searchModel.value.status,
    startTime: searchModel.value.startTime,
    endTime: searchModel.value.endTime,
    pageNo: pagination.currentPage,
    pageSize: pagination.pageSize,
  }).then((res) => {
    console.log("res ===>", res);
    if (res.data.code === 0) {
      table.data = res.data.result.items;
      table.total = parseInt(res.data.result.total);
      table.loading = false;
    }
  });
};

getData();
</script>

<style lang="less" scoped>
.attendanceRecord-page {
  .search-container {
    margin-top: 16px;
    min-height: 100%;
    padding: 24px 24px 8px 24px;
    background: #fff;
    border-radius: 6px;
  }
  .table-container {
    margin-top: 16px;
    min-height: 100%;
    padding: 24px;
    background: #fff;
    border-radius: 6px;
  }
  .table-component {
    .submit-btns {
      display: flex;
      justify-content: space-between;
      margin-bottom: 24px;
    }
    .submit-btns-right {
      display: flex;
    }
    .submit-btn-export {
      background: #35c362;
      border: 1px solid #35c362;
      border-radius: 2px;
      margin-left: 12px;
      padding: 5px 16px;
    }
    .icon-img {
      width: 16px;
      height: 16px;
      margin-right: 8px;
    }
    .table {
      margin-bottom: 15px;
      .blue {
        font-weight: 400;
        font-size: 14px;
        color: #007aff;
      }
      .red {
        font-weight: 400;
        font-size: 14px;
        color: #de2910;
      }
      .grey {
        font-weight: 400;
        font-size: 14px;
        color: #666666;
      }
    }
    .pagination {
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
</style>